import Vuex from "vuex";
import router from "./router";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import store from "./store";

const whiteList = ["/login", "/404"];
// 前置守卫
router.beforeEach(async (to, from, next) => {
  nprogress.start();

  const token = store.state.user.Token;
  console.log(token);
  if (token) {
    if (to.path === "/login") {
      next("/");
      nprogress.done();
    } else {
      next();
      // 用信息id
      if (!store.getters.userId) {
        await store.dispatch("user/getuserInfo");
      }
    }
  } else {
    if (whiteList.indexOf(to.path) > -1) {
      next();
    } else {
      next("/login");
      nprogress.done();
    }
  }
});
// 后置守卫
router.afterEach(() => {
  nprogress.done();
});
